﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SimpleMVVM.Views
{
    /// <summary>
    /// CommandWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MVVMTest : Window
    {
        public MVVMTest()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {   //【1】定义在window中这样访问，如果找不到会到父级寻找，直至全局资源
            //等同于this.FindResource["SolidColor"]
            this.Resources["SolidColor"] = new SolidColorBrush(Colors.Black);
            //查找资源
            //var solidColor = App.Current.FindResource("SolidColor");
            //solidColor = new SolidColorBrush(Colors.Blue);
            //【2】先获取mergeddictionaries遍历里面的每一个ResourceDictionary
            //再遍历该ResourceDictionary中的keys找到我们要的"Main.Background" 赋予新的值
            var resourceDictionaries = Application.Current.Resources.MergedDictionaries;
            for (int i = 0; i < resourceDictionaries.Count; i++)
            {
                foreach (var item in resourceDictionaries[i].Keys)
                {
                    string c = item.ToString();
                    if (c == "SolidColor")
                    {
                        resourceDictionaries[i][item] = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#166eae"));
                    }
                }
            }
            var style = App.Current.FindResource("DefaultButtonStyle");
            //【3】通过xaml文件更换来换样式
            //this.Resources = new ResourceDictionary()
            //{
            //    Source = new Uri("pack://Application:,,,/White.xaml")
            //    // Source = new Uri("pack://Application:,,,/Black.xaml")
            //};

        }
    }
}
